import { fetch, fetchByIdOrSid, add, addList, edit, remove } from '@/services/system';
import {  Effect } from 'dva';
import { Reducer } from 'redux';


export interface SystemModelState {
  pageData: any;
  data:any;
}
export interface SystemModelState {
  namespace: string;
  state: SystemModelState;
  effects: any;
  reducers: {
    save: Reducer<SystemModelState>;
  };
}
const SystemModel : SystemModelState  = {
  namespace: "system",
  state: {
    pageData: {
      list:[],
      pagination: {},
    },
    data: {},
  },
  effects: {

    /**======================   不对返回信息做任何处理的方法    =======================================*/

    /**
     *   基础查询
     */
    *fetch({ payload, callback },{ call, put }) {
      const response = yield call(fetch, payload);
      if(response.statusCode === "0"){
        yield put({ type: 'save', payload: { data : response }});
        if(callback) callback(response);
      }
    },
    /**
     *   分页查询
     */
    *fetchPage({ payload, callback },{ call, put }) {
      const response = yield call(fetch, payload);
      if(response.statusCode === "0"){
        const { records = [], total = 0, current = 1, size = 10 } = response.data;
        const pageData = {
            list: records,
            pagination: {
                current,
                pageSize:size,
                total
            }
        };
        yield put({ type: 'save', payload: { pageData }});
        if(callback) callback(response);
      }
    },
    /**
     *   根据id或者sid查询
     */
    *fetchByIdOrSid({ payload, callback }, { call, put }) {
      const response = yield call(fetchByIdOrSid, payload);
      if(response.statusCode === "0"){
        yield put({ type: 'save', payload: { data : response }});
        if(callback) callback(response);
      }
    },
    /**
     *  新增对象
     */
    *add({ payload, callback }, { call, put }) {
      const response = yield call(add, payload);
      if(response.statusCode === "0"){
        yield put({ type: 'save', payload: { data : response }});
        if(callback) callback(response);
      }
    },
    /**
     *  新增数组   参数名必须是list
     */
    *addList({ payload, callback }, { call, put }) {
      const response = yield call(addList, payload);
      if(response.statusCode === "0"){
        yield put({ type: 'save', payload: { data : response }});
        if(callback) callback(response);
      }
    },
    /**
     *  编辑  参数必须包含id或者sid
     */
    *edit({ payload, callback }, { call, put }) {
      const response = yield call(edit, payload);
      if(response.statusCode === "0"){
        yield put({ type: 'save', payload: { data : response }});
        if(callback) callback(response);
      }
    },
    /**
     *  删除
     */
    *remove({ payload, callback }, { call, put }) {
      const response = yield call(remove, payload);
      if(response.statusCode === "0"){
        yield put({ type: 'save', payload: { data : response }});
        if(callback) callback(response);
      }
    },

    /**==============================   对返回信息做额外处理的方法，保证数据的准确性   =============================*/



  },

  reducers: {
    save(state,action) {
      return {
        ...state,
        ...action.payload,
      };
    }
  },
};
export default SystemModel;
